<!DOCTYPE html>
<html>
<head>
    <!--purchase_drup_detail-->
	<meta charset="utf-8">
	<title>文章列表--layui后台管理模板 2.0</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="resources/layui/css/layui.css" media="all" />
	<link rel="stylesheet" href="resources/css/public.css" media="all" />
</head>
<!--查询已发货的采购单中的药品信息-->
<body class="childrenBody">
<!--采购入库模糊查询表单-->
<div class="layui-collapse" id="showPurchase">
	<blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form" id="purchaseShow" lay-filter="purchaseShow">
    	    <div class="layui-form-item layui-row">
    	        <div class="layui-inline layui-col-md6">
                    <label class="layui-form-label">采购单编号</label>
                	<div class="layui-input-block">
                	    <input type="hidden" name="id">
                		<input type="text" name="purchaseNum" id="purchaseNum" class="layui-input searchVal"  />
                	</div>
                </div>
                <div class="layui-inline layui-col-md6">
                    <label class="layui-form-label">采购单名称</label>
                	<div class="layui-input-block">
                		<input type="text" name="purchaseName" id="purchaseName" class="layui-input searchVal"  />
                	</div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-inline layui-col-md6">
                    <label class="layui-form-label">采购状态</label>
                    <div class="layui-input-block">
                    	<select name="purchaseState" id="purchaseState" lay-filter="purchaseState">
                    	    <option>请选择采购状态</option>
                    	</select>
                    	<div class="mydiv" title="不可修改" style="position:absolute;width: 100%;height: 100%;left: 0px;top: 0px;background: #fff;opacity: 0;filter:alpha(opacity=0)"></div>
                    </div>
                </div>
                <div class="layui-inline layui-col-md6">
                    <label class="layui-form-label">采购医院</label>
                	<div class="layui-input-block">
                	    <input type="hidden" name="hospitalId" id="hospitalId">
                	    <input name="hospitalName"  class="layui-input searchVal" id="hospitalName">
                	</div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-inline layui-col-md6">
                    <label class="layui-form-label">开始采购时间</label>
                	<div class="layui-input-block">
                		<input type="text" autocomplete="off" readonly id="createTime" name="createTime" class="layui-input">
                	</div>
                </div>
                <div class="layui-inline layui-col-md6">
                	<label class="layui-form-label">结束采购时间</label>
                	<div class="layui-input-block">
                    	<input type="text" autocomplete="off" readonly class="layui-input" name="overTime" id="overTime">
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-inline layui-col-md6">
                    <label class="layui-form-label">药品流水号</label>
                	<div class="layui-input-block">
                		<input type="text" name="serialNumber" class="layui-input searchVal"/>
                	</div>
                </div>
                <div class="layui-inline layui-col-md6">
                    <label class="layui-form-label">通用名</label>
                	<div class="layui-input-block">
                		<input type="text" name="commonName" class="layui-input searchVal" />
                	</div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-inline layui-col-md6">
                    <label class="layui-form-label">剂型</label>
                	<div class="layui-input-block">
                		<input type="text" name="dosageForm" class="layui-input searchVal" />
                	</div>
                </div>
                <div class="layui-inline layui-col-md6">
                    <label class="layui-form-label">规格</label>
                	<div class="layui-input-block">
                		<input type="text" name="size" class="layui-input searchVal"/>
                	</div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-inline layui-col-md6">
                    <label class="layui-form-label">单位</label>
                	<div class="layui-input-block">
                		<input type="text" name="unit" class="layui-input searchVal" />
                	</div>
                </div>
                <div class="layui-inline layui-col-md6">
                    <label class="layui-form-label">转换系数</label>
                	<div class="layui-input-block">
                		<input type="text" name="factor" class="layui-input searchVal"/>
                	</div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-inline layui-col-md6">
                    <label class="layui-form-label">生产企业</label>
                	<div class="layui-input-block">
                		<input type="text" name="productionName" class="layui-input searchVal" />
                	</div>
                </div>
                <div class="layui-inline layui-col-md6">
                    <label class="layui-form-label">商品名</label>
                	<div class="layui-input-block">
                		<input type="text" name="commodityName" class="layui-input searchVal"/>
                	</div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-inline layui-col-md6">
                    <label class="layui-form-label">药品类别</label>
                	<div class="layui-input-block">
                		<select name="drugCategory" id="drugCategoryName" lay-filter="drugCategoryName">
                            <option>请选择药品类别：</option>
                        </select>
                	</div>
                </div>
                <div class="layui-inline layui-col-md6">
                    <label class="layui-form-label">入库时间</label>
                	<div class="layui-input-inline">
                		<input type="text" autocomplete="off" readonly name="startTime" id="startTime"  class="layui-input searchVal" />
                	</div>
                	<div class="layui-input-inline layui-center" style="width: 30px;">
                	    <span>-</span>
                    </div>
                    <div class="layui-input-inline">
                    	<input type="text" autocomplete="off" readonly name="endTime" id="endTime"  class="layui-input searchVal" />
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-form-item layui-row">
                    <div class="layui-inline layui-col-md6">
                        <label class="layui-form-label">药品批号</label>
                    	<div class="layui-input-block">
                    		<input type="text" name="batchNumber" class="layui-input searchVal" />
                    	</div>
                    </div>
                    <div class="layui-inline layui-col-md6">
                        <label class="layui-form-label">发票号或入库单号</label>
                    	<div class="layui-input-block">
                    		<input type="text" name="inputForm" class="layui-input searchVal"/>
                    	</div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-inline layui-col-md6">
                    <label class="layui-form-label">供应商</label>
                	<div class="layui-input-block">
                		<select name="providerId" id="providerName" lay-filter="providerName">
                            <option>请选择供应商：</option>
                        </select>
                	</div>
                </div>
            </div>
            <div class="layui-form-item layui-center">
                <div class="layui-input-block">
                  <button type="submit" class="layui-btn" lay-submit="" lay-filter="search">查询</button>
                  <button type="submit" class="layui-btn" lay-submit="" lay-filter="output">导出</button>
                </div>
            </div>
    	</form>
	</blockquote>
</div>
<!--数据表格-->
<div>
    <table class="layui-hide" id="inputTable" lay-filter="inputTable"></table>
    <div id="toolBars" style="display: none">
        <button type="button" id="add" lay-event="add" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-add-1"></span>确认入库</button>
    </div>
</div>
<script type="text/javascript" src="resources/layui/layui.js"></script>
<script>
    layui.use(['form','jquery','layer','laydate','element','table'],function() {
        var form = layui.form;
        var laydate = layui.laydate;
        var layer = layui.layer;
        var $ = layui.jquery;
        var table = layui.table;
        //渲染下拉框、时间
        laydate.render({
            elem: '#inputTime'
            ,type: 'datetime'
        });
        laydate.render({
            elem: '#createTime'
            ,type: 'date'
        });
        laydate.render({
            elem: '#overTime'
            ,type: 'date'
        });
        laydate.render({
            elem: '#startTime'
            ,type: 'datetime'
        });
        laydate.render({
            elem: '#endTime'
            ,type: 'datetime'
        });
        //采购状态
        select($("#purchaseState"),3,2);
        //药品类别
        selectDrugCatalog($("#drugCategoryName"));
        //供应商
        selectProvider($("#providerName"));
        //获得医院ID
        var hospitalId;
        $.ajax({
            url:"/getSessionUnitId",
            async:false,
            type:"post",
            success:function(unitId) {
                hospitalId = unitId;
                $("#hospitalId").val(hospitalId);
            }
        });
        //记录采购状态
        var purchaseState = 2;
        //监听采购状态的下拉框
        form.on('select(purchaseState)', function(data){
            purchaseState = data.value;
        });
        //医院存在
        if(hospitalId!=undefined&&hospitalId!=null){
            $.get("/hospital/getHospitalById",{id:hospitalId},function(data) {
                $("#hospitalName").val(data.hospitalName);
                $("#hospitalName").attr("readonly","readonly");
            })
        }else {//未登录
            window.location.href = "/toNoPermission";
        }
        //渲染数据表格
        var tableIns = table.render({
            elem:"#inputTable",
            url:"/getInputPurchaseDrupDetail",
            page:true,
            id:"inputTableId",
            where:{hospitalId:hospitalId,purchaseState:purchaseState},
            totalRow: true,
            toolbar:"#toolBars",
            cols:[
                [
                     {field:'id', title:'ID',totalRowText:"合计",  fixed: 'left', unresize: true,width:'80' ,sort: true,align:'center'},
                     {type:'checkbox',fixed:'center'},
                     {field:'purchase.purchaseNum', title:'采购单编号',align:'center',width:'150',templet:function(res) {
                        return res.purchase==null?"":res.purchase.purchaseNum;
                     }},
                     {field:'purchase.purchaseName', title:'采购单名称',align:'center',width:'150',templet:function(res) {
                        return res.purchase==null?"":res.purchase.purchaseName;
                     }},
                     {field:'drugMessage.serialNumber', title:'流水号',align:'center',width:'150',templet:function(res) {
                        return res.drugMessage==null?"":res.drugMessage.serialNumber
                     }},
                     {field:'drugMessage.commonName',sort:true, title:'通用名',align:'center',width:'150',templet:function(res) {
                        return res.drugMessage==null?"":res.drugMessage.commonName
                     }},
                     {field:'drugMessage.commodityName', title:'商品名',align:'center',width:'150',templet:function(res) {
                        return res.drugMessage==null?"":res.drugMessage.commodityName;
                     }},
                     {field:'drugMessage.winPrice', title:'中标价',align:'center',width:'150',templet:function(res) {
                        return res.drugMessage==null?"":res.drugMessage.winPrice;
                     }},
                     {field:'trading', title:'交易价',align:'center',width:'150',templet:function(res) {
                        return res.trading==null?"":res.trading;
                     }},
                     {field:'purchaseAmount',totalRow: true, title:'采购量',align:'center',width:'150',templet:function(res) {
                        return res.purchaseAmount==null?"":res.purchaseAmount;
                     }},
                     {field:'purchasePrice',totalRow: true, title:'采购金额',align:'center',width:'150',templet:function(res) {
                        return res.purchasePrice==null?"":res.purchasePrice;
                     }},
                     {field:'input.inputNum',totalRow: true, edit:true, title:'入库量',align:'center',width:'150',templet:function(res) {
                        return res.input==null?"":res.input.inputNum==null?"":res.input.inputNum;
                     }},
                     {field:'input.inputPrice' ,totalRow: true, title:'实际交易金额',align:'center',width:'150',templet:function(res) {
                        return res.input==null?"":res.input.inputPrice==null?"":res.input.inputPrice;
                     }},
                     {field:'input.inputForm',edit:true, title:'发票号或入库单号',align:'center',width:'150',templet:function(res) {
                        return res.input==null?"":res.input.inputForm==null?"":res.input.inputForm;
                     }},
                     {field:'input.batchNumber',edit:true, title:'药品批号',align:'center',width:'150',templet:function(res) {
                        return res.input==null?"":res.input.batchNumber==null?"":res.input.batchNumber;
                     }},
                     {field:'input.drugTime',edit:true, title:'药品有效期',align:'center',width:'150',templet:function(res) {
                        return res.input==null?"":res.input.drugTime==null?"":res.input.drugTime;
                     }},
                     {field:'drugMessage.dosageForm', title:'剂型',align:'center',width:'150',templet:function(res) {
                        return res.drugMessage==null?"":res.drugMessage.dosageForm;
                     }},
                     {field:'drugMessage.size', title:'规格',align:'center',width:'150',templet:function(res) {
                        return res.drugMessage==null?"":res.drugMessage.size;
                     }},
                     {field:'drugMessage.unit', title:'单位',align:'center',width:'150',templet:function(res) {
                        return res.drugMessage==null?"":res.drugMessage.unit;
                     }},
                     {field:'drugMessage.factor', title:'转换系数',align:'center',width:'150',templet:function(res) {
                        return res.drugMessage==null?"":res.drugMessage.factor;
                     }},
                     {field:'drugMessage.productionName', title:'生产企业',align:'center',width:'150',templet:function(res) {
                        return res.drugMessage==null?"":res.drugMessage.productionName;
                     }},
                     {field:'input.inputTime', title:'入库时间',align:'center',width:'150',templet:function(res) {
                        return res.input==null?"":res.input.inputTime==null?"":res.input.inputTime;
                     }},
                     {field:'purchaseState', title:'采购状态',align:'center',width:'150',templet:function(res) {
                        return res.purchaseState==1?"未确认送货":
                        res.purchaseState==2?"已发货":
                        res.purchaseState==3?"已入库":
                        res.purchaseState==4?"无法供货":
                        res.purchaseState==5?"到期未供货":"";
                     }}
                ]
            ]
        });
        //监听表格被编辑
        table.on('edit(inputTable)',function(obj) {
            console.log(obj.data);
            console.log(obj.value); //得到修改后的值
            console.log(obj.field); //当前编辑的字段名
            switch (obj.field) {
                case 'input.inputNum':
                    obj.data.input.inputNum = obj.value;
                    obj.data.input.inputPrice = obj.value * obj.data.trading;
                    break;
                case 'input.inputForm':
                    obj.data.input.inputForm = obj.value;
                    break;
                case 'input.inputPrice':
                    obj.data.input.inputPrice = obj.value;
                    break;
                case 'input.batchNumber':
                    obj.data.input.batchNumber = obj.value;
                    break;
                case 'input.drugTime':
                    obj.data.input.drugTime = obj.value;
                    break;
            }
        });
        //监听头部工具栏事件
        table.on('toolbar(inputTable)',function(obj) {
            switch (obj.event) {
                case 'add':
                    openAdd();
            }
        });
        //表单查询
        form.on('submit(search)',function(data) {
            console.log(data.field)
            tableIns.reload({
                where:data.field
            });
            if(purchaseState!=2){
                $("#add").hide();
            }else {
                $("#add").show();
            }
            return false;
        })
        form.on('submit(output)',function() {
            var checkStatus = table.checkStatus('inputTableId'); //idTest 即为基础参数 id 对应的值
            var data = checkStatus.data;
            if(data.length==0){
                layer.msg("请先勾选");
                return false;
            }
            table.exportFile(tableIns.config.id,data,'xls');
            return false;
        })
        //确认入库
        function openAdd() {
            //已发货才可以采购
            if(purchaseState==2){
                var checkStatus = table.checkStatus('inputTableId'); //idTest 即为基础参数 id 对应的值
                var data = checkStatus.data;
                if(data.length==0){
                    layer.msg("请先勾择要入库的数据");
                    return;
                }
                var inputs = new Array();
                var isYet = true;
                $.each(data,function(index,item) {
                    if(item.input.inputNum!=null&&item.input.inputPrice!=null&&
                    item.input.batchNumber!=null&&item.input.inputForm!=null&&item.input.drugTime!=null){
                        inputs.push(item);
                    }else {
                        layer.msg("请完善入库单信息");
                        isYet = false;
                    }
                });
                if(isYet){
                    //添加入库单，修改采购状态
                    $.post("/hpi/add",{purchaseStr:JSON.stringify(inputs),},function(data) {
                        layer.msg(data.msg+":"+data.num+",失败:"+(inputs.length-data.num));
                        tableIns.reload();
                    })
                }
            }
        }
        //数据字典下拉框
        function select(item,dateType,num) {
            $.get("/getDataDictionary",{dateType:dateType},function(data) {
                var html = "";
                $.each(data.data,function(index,item) {
                    if(item.dataNum==num){
                        html += "<option selected value='"+item.dataNum+"'>"+item.dataName+"</option>";
                    }else {
                        html += "<option value='"+item.dataNum+"'>"+item.dataName+"</option>";
                    }
                })
                item.empty();
                item.append(html);
                // $("#purchaseState").html(html);
                form.render('select');
            })
        }
        //药品类别下拉框
        function selectDrugCatalog(item) {
            $.get("/getDrugCategory",function(data) {
                var html = "<option value='0'>请选择药品类别：</option>";
                $.each(data,function(index,item) {
                    html += "<option value='"+item.id+"'>"+item.category+"</option>";
                })
                item.html(html);
                form.render('select');
            })
        }
        //供应商下拉框
        function selectProvider(item) {
            $.get("/provider/getProvider",function(data) {
                var html = "<option value='0'>请选择供应商：</option>";
                $.each(data,function(index,item) {
                    html += "<option value='"+item.id+"'>"+item.providerName+"</option>";
                })
                item.html(html);
                form.render('select');
            })
        }

    });
</script>
</body>